layui.config({
  base: '/home_v2/common/',
}).extend({
  index: '/lib/index',
  soulTable: 'soulTable'
}).use(['table', 'index', 'soulTable', 'form', 'laytpl','laypage'],function () {
   
  let soulTable = layui.soulTable,
  form = layui.form,
  table = layui.table,
  laytpl = layui.laytpl,
  laypage = layui.laypage,
  element = layui.element;

var oneTypeJson=[];
//用户范围
form.on('checkbox(yhfw1)', function(data) {
  var twoTypeJson=[];
  if($("#yhfw").val() !=""){
    twoTypeJson = $("#yhfw").val().split(",");
  }
  if (data.elem.checked) {
    twoTypeJson.push(data.elem.value)
  } else {
    var SJson = [];
    for (var i in twoTypeJson) {
      if (twoTypeJson[i] != data.elem.value) {
        SJson.push(twoTypeJson[i])
      }
    }
    twoTypeJson = SJson;
  }
  $("#yhfw").val(twoTypeJson.join(","))
})
form.on('switch(fallSwitch)', function(data){
  if(data.elem.checked){
    $("#isStop").val(0)
  }else {
    $("#isStop").val(1)
  }
})

form.on('submit(sub_button)', function (data) {
  console.log("提交",data.field)
});


// var checkedValues = ['写作', '发呆'];
// setChecked('yhfw1', checkedValues);

//设置复选框选中状态
function setChecked(name,data){
  let json = {}
  for(let i=0;i<data.length;i++){
    json[name+"['"+data[i]+"']"] = true
  }
  form.val('editForm', json);
  form.render("checkbox")
}
  
  
  //表格渲染
      /**
       *
       * @param {*} data 默认参数为初次渲染的选中值。该函数每次调用渲染一次列表，传入不同的参数
       */
      function tableRenderFun(data = {}) {
        let projectId = $(".tree-level-1-item.lay-sent").attr("data-id");
        projectId = projectId ? projectId : "";
        let searchValue = $(".table-search-value").val();
        table.render({
          elem: "#tableList", //渲染
          method: "post", //请求方式
          // , height:  //表格高度
          id: "tableList",
          url: "/sharing/md-api-generate/getPage", //替换接口
          defaultToolbar: [], //工具栏右侧隐藏
          title: "单位信息表",
          even: true,
          response: { statusCode: 200 },
          parseData: function (res) {
            console.log(res); //res 即为原始返回的数据
            return {
              code: res.code, //解析接口状态
              msg: res.msg, //解析提示文本
              count: res.data.total, //解析数据长度
              data: res.data.records, //解析数据列表
            };
          },
          cols: [
            [
              //表格绑数据  需要根据字段修改绑定值
              { type: "checkbox", width: "5%", fixed: "left" },
              { type: "numbers", title: "序号", align: "center", width: "5%" },
              { field: "name", title: "名称", align: "center", width: "20%" },
              {
                field: "apiMode",
                title: "模式",
                align: "center",
                templet: function (d) {
                  return d.apiMode == 1 ? "向导模式" : "sql模式";
                },
              },
              { field: "apiPath", title: "路径", align: "center" },
              {
                field: "operate",
                title: "操作",
                align: "center",
                fixed: "right",
                width: "13%",
                templet: function (d) {
                  // return config_v2.listbut("编辑,删除", "", "", "edit,dele");
                  return (
                    '<span class="layui-btn opration_btn  opration-btn-edit " lay-event="edit"><i class=\'icon iconfont\'>&#xe613;</i>编辑</span>' +
                    '<span class="layui-btn opration_btn opration-btn-dele " lay-event="dele"><i class=\'layui-icon layui-icon-delete\'></i>删除</span>'
                  );
                },
              },
            ],
          ],
          request: {
            pageName: "currentPage", //页码的参数名称，默认：page
            limitName: "pageSize", //每页数据量的参数名，默认：limit
          },
          contentType: "application/json", //格式
          headers: config_v2.headers("getPage"),
          page: pageLimit, //开启分页
          limits: [20, 50, 100, 200, 500], //控制多少行一页（默认五条一页）
          limit: 10,
          where: {
            searchValue: searchValue,
            // projectId:''
          },
          done: function () {
            config_v2.tablelist();
            console.log("chonzhi");
            Window.table = table;
            table.resize("tableList");
          },
        });
        // table.off("tool(tableList)")，监听列表操作按钮的点击事件
        table.on("tool(tableList)", function (e) {
          switch (e.event) {
            case "edit":
              {
                let id = e.data.id;
                // 此处需要传入id值，根据接口的id字段修改以 id=value形式
                let url = "/page/metadata_manage/favorite_addOrEdit.html";
                layer.open({
                  id: "edit",
                  type: 2,
                  title: false,
                  // closeBtn:0,
                  resize: true,
                  maxmin: false,
                  closeBtn: 0,
                  skin: "layui-layer-rim", //加上边框
                  area: ["100%", "100%"], //宽高
                  // btn: ["确定", "取消"],
                  content: [url, "yes"], //调到编辑页面
                  success: function () {
                    $(".layui-layer-title").addClass("laytitle");
                  },
                });
              }
              break;
            case "dele":
              {
                let id = e.data.id;
                layer.confirm("确定删除吗？", function (index) {
                  delFun(id);
                });

                function delFun(id) {
                  config_v2.delete_ajax(
                    "/sharing/md-api-generate/delOne/" + id,
                    {
                      // contentType: "application/json;charset=UTF-8",
                      success: function (res) {
                        if (res.code === 200) {
                          table.reload("tableList");
                        }
                      },
                      error: function (res) {
                        layer.msg(res.data, { icon: 5 });
                      },
                    },
                    id
                  );
                }
              }
              break;
          }
        });
      }

      //处理新增按钮事件
      $(document).on("click", ".btn-add", function (e) {
        // 若需要插入的数据为单条表数据，传入id值.获取到第二个下拉列表的标签中data-id,传入即可
        // parent.open("/page/metadata_manage/favorite_addOrEdit.html?isEdit=true")
        layer.open({
          id: "add",
          type: 2,
          title: false,
          // closeBtn:0,
          resize: false,
          maxmin: false,
          closeBtn: 0,
          skin: "layui-layer-rim", //加上边框
          area: ["100%", "100%"], //宽高
          // btn: ["确定", "取消"],
          content: [
            "/page/metadata_manage/favorite_addOrEdit.html?isEdit=true",
            "yes",
          ], //调到新增页面
        });
      });
      //处理新增按钮事件
      $(document).on("click", ".btn-del", function (e) {
        // 在此处调用删除接口，传入需要删除的数据id
        let id = e.data.id;
        layer.confirm("确定删除吗？", function (index) {
          delFun(id);
        });
      });
  
})
